package org.nidy.erp.service.impl;

import org.nidy.erp.mapper.ILoginMapper;
import org.nidy.erp.pojo.Employees;
import org.nidy.erp.service.ILoginService;
import org.nidy.erp.utils.JwtUtils;
import org.nidy.erp.utils.MD5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class LoginService implements ILoginService {
    @Autowired
    private ILoginMapper loginMapper;
    @Override
    public Employees checkLogin(Employees employees) {
        // password进行Md5加密后进行校验
        String password = employees.getPassword();
        String encrypt = MD5.encrypt(password).toUpperCase();
        employees.setPassword(encrypt);
        Employees emp = loginMapper.checkLogin(employees);
        if (emp != null) {
            log.info("登录校验返回结果:{}", emp);
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", emp.getId());
            claims.put("loginid", emp.getLoginid());
            claims.put("lastname", emp.getLastname());
            String jwt = JwtUtils.generateToken(claims);
            return new Employees(emp.getId(),emp.getLoginid(),emp.getLastname(),jwt);
        }
        return null;
    }
}
